﻿using UnityEngine;
using System.Collections;

public class UnitWindow : MonoBehaviour {

	GameObject UnitNote;
	GameObject UnitNoteParent;
    GameObject UnitNoteScroll;

    bool bOnce;
	void Awake(){
		UnitNote = Resources.Load ("Prefaps/UI/StaticUI/UnitNote/UnitNote") as GameObject;
		UnitNoteParent = Resources.Load ("Prefaps/UI/StaticUI/UnitNote/UnitNoteParent") as GameObject;
        UnitNoteScroll = Resources.Load("Prefaps/UI/StaticUI/UnitNote/UnitNoteScroll") as GameObject;
	}

	void OnEnable(){
        
		(Instantiate (UnitNoteParent) as GameObject).transform.parent = transform;
		transform.FindChild ("UnitNoteParent(Clone)").transform.localPosition = Vector3.zero;
		transform.FindChild ("UnitNoteParent(Clone)").transform.localScale = Vector3.one;

        for (int i = 0; i < GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().UnitList.Count; ++i)
        {
			(Instantiate (UnitNote)as GameObject).transform.parent = transform.FindChild("UnitNoteParent(Clone)");
			transform.FindChild("UnitNoteParent(Clone)").GetChild (i).GetComponent<UnitNote>().iIndex = i;
			transform.FindChild("UnitNoteParent(Clone)").GetChild (i).transform.localPosition = new Vector3 (0.00f, -10.00f -  i*17);
			transform.FindChild("UnitNoteParent(Clone)").GetChild (i).transform.localScale = new Vector3 (0.7f, 1.0f);
			transform.FindChild("UnitNoteParent(Clone)").GetChild (i).GetComponent<UISprite> ().depth = GetComponent<UISprite> ().depth + 3;
		}

	}

	void Update(){
        if (GameObject.Find("RightWdw").GetComponent<RightWdwMgr>().m_bSelectUnitPage && !bOnce)
        {
            transform.GetChild(1).gameObject.SetActive(true);
            transform.parent.parent.GetComponent<RightWdwMgr>().m_bSelectUnitPage = true;

            bOnce = true;
        }
	}

	void OnDisable(){
        GameObject.Find("RightWdw").GetComponent<RightWdwMgr>().m_bSelectUnitPage = false;
        transform.GetChild(1).gameObject.SetActive(false);
        bOnce = false;

		for (int i = 0; i < GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().UnitList.Count; ++ i) {
			transform.FindChild("UnitNoteParent(Clone)").GetChild (i).GetComponent<UnitNote> ().DestroyThis ();
		}
		Destroy (transform.FindChild ("UnitNoteParent(Clone)").gameObject);

        transform.GetChild(0).gameObject.SetActive(false);
	}
}
